<template>
  <!-- 医生信息页顶部条 -->
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size: 20px;">医生管理</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="dialogVisible=true">添加医生</el-button>
  </div>

  <!-- 医生查询卡片 -->
  <el-card style="margin: 20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="医生搜索">
        <el-input placeholder="请输入医生姓名" style="width: 220px;" v-model="searchDoctorForm.nickname"
                  @keydown.enter="loadDoctor"></el-input>
      </el-form-item>

      <el-form-item label="医生职位" style="width: 290px;">
        <el-select placeholder="请选择医生职位" v-model="searchDoctorForm.job" @change="loadDoctor">
          <el-option v-for="item in levelOptions"
                     :label="item.label" :value="item.value"></el-option>
          <!--          <el-option label="执业医师" value="10"></el-option>-->
          <!--          <el-option label="主治医师" value="20"></el-option>-->
          <!--          <el-option label="副主任" value="30"></el-option>-->
          <!--          <el-option label="主任" value="40"></el-option>-->
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadDoctor">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <!-- 显示医生信息表格 -->
  <el-card style="margin:20px;">
    <el-table :data="doctorArr">
      <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
      <el-table-column prop="nickname" label="医生姓名" align="center"></el-table-column>
      <el-table-column prop="job" label="医生职位" align="center" :formatter="doctorJobFormatter"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editDoctor(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="resetPassword(scope.row.id)">密码重置</el-button>
          <el-button link type="primary" size="small" @click="deleteDoctor(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <!--添加医生弹窗-->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" :before-close="handleClose"
             style="width:1000px;padding:40px;">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="50">
        <el-col :span="12">
          <el-form-item label="医生账号">
            <el-input placeholder="请输入医生账号" v-model="saveDoctorForm.username"
                      :disabled="saveDoctorForm.id!=null"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="医生密码">
            <el-input placeholder="请设置医生密码" v-model="saveDoctorForm.password"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="50">
        <el-col :span="12">
          <el-form-item label="医生姓名">
            <el-input placeholder="请输入医生姓名" v-model="saveDoctorForm.nickname"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="医生职位">
            <el-select placeholder="请选择医生职位" v-model="saveDoctorForm.job">
              <el-option v-for="item in levelOptions" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button @click="saveDoctor" type="primary">保存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";

//定义数组用来装医生列表数据
const doctorArr = ref([]);
//定义对象保存查询条件
const searchDoctorForm = ref({
  nickname: '',
  job: '',
});
//重置医生搜索信息
const resetSearch = () => {
  searchDoctorForm.value = {};//清空医生搜索表单数据
  loadDoctor();//重新加载医生数据
}

//定义加载用户的方法
const loadDoctor = () => {
  //有条件则根据条件查医生数据,没有条件默认查所有医生数据
  let data = qs.stringify(searchDoctorForm.value);
  axios.get(BASE_URL + '/v1/admin/doctor/select?' + data).then((response) => {
    if (response.data.code == 2000) {
      console.log(response.data.data);
      doctorArr.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg);
    }
  })
};

//页面一加载立即请求加载医生数据
onMounted(() => {
  loadDoctor();
});

//定义数组维护医生职位
const levelOptions = ref([
  {label: '执业医师', value: '10'},
  {label: '主治医师', value: '20'},
  {label: '副主任', value: '30'},
  {label: '主任', value: '40'}
]);
//准备对象用来收集新增/修改医生的表单信息
const saveDoctorForm = ref({
  username: '',
  password: '',
  job: '',
  nickname: '',
  role: ''
});

//医生职位显示转换
const doctorJobFormatter = (row, column, cellValue, index) => {
  if (!levelOptions.value) {
    return cellValue;
  }
  for (let item of levelOptions.value) {
    if (item.value == cellValue) {
      cellValue = item.label;
    }
  }
  return cellValue;
};
//定义变量控制弹窗标题
const dialogTitle = ref('添加医生');

//定义变量控制弹窗是否显示
const dialogVisible = ref(false);

//实现关闭功能
const handleClose = () => {
  if (confirm('是否关闭本窗口?')) {
    //用户确认关闭后,将弹窗关闭且将表单数据清空
    dialogVisible.value = false;
    saveDoctorForm.value = {};
  }
};

//添加医生
const saveDoctor = () => {
  let data = qs.stringify(saveDoctorForm.value);
  console.log(data);
  axios.post(BASE_URL + '/v1/admin/save', data).then((response) => {
    if (response.data.code == 2000) {
      ElMessage.success('保存成功!');
      dialogVisible.value = false;//关闭弹窗
      saveDoctorForm.value = {};//清空表单输入数据,防止新增成功后还是上一个用户的数据
      loadDoctor();
    } else {
      ElMessage.error(response.data.msg);
    }
  })
};

//编辑医生
const editDoctor = (doctorId) => {
  dialogVisible.value = true;//显示弹窗
  dialogTitle.value = '编辑医生';//修改弹窗标题
  //请求当前要编辑的医生信息
  axios.get(BASE_URL + '/v1/admin/selectByDoctorId?id=' + doctorId).then((response) => {
    if (response.data.code == 2000) {
      //将请求到的医生数据回显在弹窗表单中
      saveDoctorForm.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg);
    }
  })
};

//删除医生方法
const deleteDoctor = (doctorId) => {
  if (confirm('您确认要删除该医生吗?')) {
    axios.post(BASE_URL + '/v1/admin/deleteDoctor/' + doctorId).then((response) => {
      if (response.data.code == 2000) {
        ElMessage.success('删除成功!');
        loadDoctor();
      } else {
        ElMessage.error(response.data.msg);
      }
    })
  }
};

//修改医生密码
const resetPassword = (doctorId) => {
  axios.post(BASE_URL + '/v1/admin/resetDoctorPassword/' + doctorId).then((response) => {
        if (response.data.code == 2000) {
          ElMessage.success('重置成功!')
        } else {
          ElMessage.error(response.data.msg)
        }
      })
};
</script>

<style scoped>

</style>